<?php


namespace api\blog\controller;


use api\blog\model\UserModel;
use api\blog\validate\UserValidate;
use cmf\controller\RestBaseController;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use think\facade\Filesystem;

class UploadController extends RestBaseController
{
    //查询数据
    public function list(){
        $mobile = input('mobile');
        $list = UserModel::where('mobile',$mobile)->find();
        if($list){
            $this->success('查询成功',[
                'data' => $list
            ]);
        }
    }
    //本地上传
    public function local(){
        $file = request()->file('file');
        $res = Filesystem::disk('public')->putFile('image',$file);
        if($res){
            $this->success('upload success',[
                'url' => request()->domain().'/stotage/'.$res
            ]);
        }
    }
    //头像、上传图片,文件
    public function uploadFile(){
        //获取表单上传文件
        $file = request()->file('file');
        //上传到本地的服务器
        $result = Filesystem::disk('public')->putFile('file',$file);
        if($result){
            $this->success('上传成功',['url'=>request()->domain().DIRECTORY_SEPARATOR.'storage'.DIRECTORY_SEPARATOR.$result]);
        }
        $this->error('上传失败');
    }
    //更新信息
    public function adminAdd()
    {
        $header = request()->header();
        if(!isset($header['token']) || empty($header['token'])){
            $this->error('缺少token!');
        }
        $key = new Key(config('app.jwt_key'),'HS256');
        try{
            $res = JWT::decode($header['token'],$key);
        }catch (\Exception $exception){
            $this->error($exception->getMessage());
        }
        $name = input('name');
        $mobile = input('mobile');
        $image = input('image');
        $res = UserModel::where('mobile','=',$mobile)
            ->update([
                'image'=>$image,
                'name'=>$name,
                'mobile' => $mobile
            ]);
        if(!empty($res))
        {
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }
}